<#import "/common/function.ftl" as fn>
<@fn.header _title="">
<#import "/spring.ftl" as spring />
<script type="text/javascript" src="${base}/resources/script/jquery.validate.js"></script>
<script type="text/javascript" src="${base}/resources/script/jquery.validate.methods.js"></script>
<script type="text/javascript" src="${base}/resources/script/date97/WdatePicker.js"></script>
<script type="text/javascript">
$(function() {
	var $validateErrorContainer = $("#validateErrorContainer");
	var $validateErrorLabelContainer = $("#validateErrorContainer ul");
	var $validateForm = $("#validateForm");
	
	// 表单验证
	$validateForm.validate({
		errorContainer: $validateErrorContainer,
		errorLabelContainer: $validateErrorLabelContainer,
		wrapper: "li",
		errorClass: "validateError",
		ignoreTitle: true,
		rules: {
			"code": "required",
			"name": "required",
		},
		messages: {
			"code": "请填写编号",
			"name": "请填写名称",
		},
		submitHandler: function(form) {
			$(form).find(":submit").attr("disabled", true);
			form.submit();
		}
	});	
});

function getListByType()
{
	if($("#regionType").val() == 1)
	{
		var html = '<select name="parentId"><option value="0">沈阳市</option></select>';
		$("#parentId").html(html);
	}
	else
	{
		$.ajax({
			url: 'getListByType',
			type: 'POST',
			data: {type:$("#regionType").val()},
			dataType: 'json',
			timeout: 1000,
			error: function(){
				alert('服务器响应失败');
			},
			success: function(data){
				var html = '<select name="parent.id"><option value="">请选择所属行政区域</option>';
				for(var i = 0; i < data.regionList.length; i++)
				{
					html = html + '<option value="'+data.regionList[i].id+'">' + data.regionList[i].name + '</option>';
				}
				html = html + '</select>';
				$("#parentId").html(html);
			}
		});
	}
	if($("#regionType").val() != ""){
		$("#trParentId").show();
	}else{
		$("#trParentId").hide();
	}
}
</script>
</@fn.header>

<body style="width:100%" class="list">
<form id="validateForm" action="<#if isAddAction>save<#else>update</#if>" method="post">
	<h2><img src="${base}/resources/style/images/iconDivM.gif" width="16" height="16" />机构管理</h2>
	<div id="validateErrorContainer" class="validateErrorContainer marginTop15" style="display:none;">
		<div class="validateErrorTitle">以下信息填写有误,请重新填写</div>
		<ul></ul>
	</div>
	<div class="title marginTop15">
		<div class="title-middle">
			<div class="title-left"></div>
			<div class="title-caption"><#if isAddAction>添加机构信息<#else>编辑机构信息</#if></div>
            <div class="title-right"></div>
		</div>
	</div>
	<table class="table3" id="userListTable">
		<tr>
			<th>
				名称: 
			</th>
			<td>
				<input type="text" name="name" class="formText" value="${(region.name)!}" />
				<label class="requireField"><font style="font-weight:bold">${(error1)!}</font>*</label>
			</td>
		</tr>
		<tr>
			<th>
				编号: 
			</th>
			<td>
				<input type="text" name="code" class="formText" value="${(region.code)!}" />
				<label class="requireField"><font style="font-weight:bold">${(error2)!}</font><#if region!=null><@spring.bind "region.code" /><@spring.showErrors "br"/><#else>*</#if></label>
			</td>
		</tr>
		<tr>
			<th>
				机构级别: 
			</th>
			<td>
				<select id="regionType" name="regionType" onchange="getListByType();">
					<option value="">请选择机构级别</option>
					<option value="1">市</option>
					<option value="2">区县</option>
					<option value="3">街道</option>
					<option value="4">小区</option>
				</select>
			</td>
		</tr>
		<tr id="trParentId" style="display:none;">
			<th>
				所属机构: 
			</th>
			<td id="parentId">
				<select name="parent.id">
					<option value="">请选择机构级别</option>
				</select>
			</td>
		</tr>
	</table>
	<input type="submit" class="button" value="确 定" />
	<input type="button" class="button" value="返 回" onclick="javascript:history.go(-1);" />
</form>
</body>
</html>